import headerWrap from "../commonComponents/header.vue"
import noDataWrap from "../commonComponents/noMoreData.vue"
import loadingWrap from "../commonComponents/loadingInPage.vue"
import switchTab from "../commonComponents/switchTab.vue"

_methods.myTaskLIstRefresh = () => {
  let arr = [_data.myTaskPendingList, _data.myTaskCompleteList, _data.myTaskOverList];
  _methods.meScrollTriggerDownScrollArr(arr);
};

//feedbackType 2预警 3事件

export default {
  data() {
    return {
      type: 1,
      show: true,
      switchFlag: 0,
      tabData: ["在办", "已办", "办结"],
      pendingPageIndex: 0,
      completePageIndex: 0,
      overPageIndex: 0,
      pendingList: [],
      completeList: [],
      overList: [],
      detailPath: null,
    }
  },
  methods: {
    init() {
      _methods.headerTitleChange(9);
      setTimeout(() => {
        this.pendingListInit();
        this.completeListInit();
        this.overListInit();
      }, 200);
    },
    pendingListInit() {
      let self = this;
      _data.myTaskPendingList = _methods.initMeScroll("pending", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          warpId: "pending",
          callback: () => {
            self.getList(1, 2);
          }
        }
      });
    },
    completeListInit() {
      _data.myTaskCompleteList = _methods.initMeScroll("complete", {
        //刷新
        down: {
          callback: () => {
            this.getList(2, 1);
          }
        },
        //加载更多
        up: {
          warpId: "complete",
          callback: () => {
            this.getList(2, 2);
          }
        }
      });
    },
    overListInit() {
      _data.myTaskOverList = _methods.initMeScroll("over", {
        //刷新
        down: {
          callback: () => {
            this.getList(3, 1);
          }
        },
        //加载更多
        up: {
          warpId: "over",
          callback: () => {
            this.getList(3, 2);
          }
        }
      });
    },
    //列表
    getList(listFlag, scrollFlag) {
      /*
       listFlag：1.待办 2.已办 3.办结
       scrollFlag：1：刷新 2：加载更多
       */
      let self = this;
      //列表参数
      let listKey = {
        1: "pendingList",
        2: "completeList",
        3: "overList",
      }[listFlag];
      //scrollKey
      let scrollKey = {
        1: "myTaskPendingList",
        2: "myTaskCompleteList",
        3: "myTaskOverList",
      }[listFlag];
      //页码参数
      let pageIndex = {
        1: "pendingPageIndex",
        2: "completePageIndex",
        3: "overPageIndex",
      }[listFlag];

      //待办 已办 办结，类型
      let statusList = {
        1: ["0"],
        2: ["1"],
        3: ["2"],
      }[listFlag];

      let data = {
        orgID: _data.userInfo.deptId,
        size: _data.pageSize,
        statusList:JSON.stringify(statusList),
        feedbackType:"",
      };

      // data.statusList.push(statusList);

      //ajax获取列表数据
      let ajData = (s, pg = 1) => {
        data.page = pg;
        _ajax.noLoading(_ajaxUrls.selfTasks.url,
          data,
          (v) => {
          if (v.result){
            let val = self.listFormat(v.result.records);
            let len = val.length;
            if (s === 1) {
              //刷新
              self[listKey] = val;
            } else {
              //加载更多
              let arr = this[listKey];
              val.forEach((i) => {
                arr.push(i);
              });
              self[listKey] = arr;
            }
            _data[scrollKey].endBySize(len, _data.pageSize);
          }
        },
          _ajaxUrls.selfTasks.type);
      };

      if (scrollFlag === 1) {
        //刷新列表
        self[pageIndex] = 1;
        ajData(scrollFlag, self[pageIndex]);
      } else {
        // 加载更多
        self[pageIndex] = parseInt(self[pageIndex]) + 1;
        ajData(scrollFlag, self[pageIndex]);
      }
    },
    go_details(item) {
      let flag = parseInt(item.feedbackType);

      let path = {
        // 1: `/myTaskList/infoDetail?id=${item.id}&type=2&isMyTask=1`,
        2: `/myTaskList/riskDetail?id=${item.id}&listType=2&type=3&flag=1`,
        3: `/myTaskList/riskDetail?id=${item.id}&listType=2&type=2&flag=1`,
      }[flag];

      _methods.goPath(path);
    },
    switchClick(i) {
      this.switchFlag = i;
    },
    listFormat(arr) {
      //feedbackType 2预警 3事件
      let type = {
        1: "信息",
        2: "预警",
        3: "事件",
      };
      return arr.map((i) => {
        let key = {
          2:"reRisk",
          3:"reEvent",
        }[i.feedbackType];
        i.title = i[key].title;
        i.typeTxt = type[i.feedbackType - 0];
        i.time = i[key].happenTime;
        return i;
      });
    }
  },
  components: {
    headerWrap,
    noDataWrap,
    loadingWrap,
    switchTab,
  },
  mounted() {
    window._methods.isNotPush().then(() => {
      this.init();
    });
  }
}
